home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / PGP / source / pgp50i-b8a / tools / crc.c next >
Encoding:
C/C++ Source or Header  |  1998-01-04  |  2.3 KB  |  85 lines

  1. /*
  2.  * crc.c -- Cyclic redundancy checksum utilities
  3.  *
  4.  * Copyright (C) 1997 Pretty Good Privacy, Inc.
  5.  *
  6.  * Written by Mark H. Weaver
  7.  *
  8.  * $Id: crc.c,v 1.4 1997/07/05 21:04:33 mhw Exp $
  9.  */
  10.  
  11. #include "crc.h"
  12.  
  13. /* CRC-32: X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X+1 */
  14. #define CRC32_POLY    0xedb88320
  15.  
  16. /* CRC-CCITT: X^16 + X^12 + X^5 + 1 */
  17. #define CRC16_POLY    0x8408
  18.  
  19. /*
  20.  * Templated Optimized CRC functions
  21.  */
  22.  
  23. #define DECLARE_CRC_TABLE(wordXX, crcXXTable)                                \
  24.     wordXX    crcXXTable[0x100];
  25.  
  26. #define DEFINE_INIT_CRC_TABLE_FUNC(InitCRCXXTable, CRCXX_POLY,                \
  27.                                    wordXX, crcXXTable)                        \
  28.     void InitCRCXXTable(void)                                                \
  29.     {                                                                        \
  30.         int        i, oneBit;                                                    \
  31.         wordXX    crc = 1;                                                    \
  32.                                                                             \
  33.         for (oneBit = 0x80; oneBit > 0; oneBit >>= 1) {                        \
  34.             crc = (crc >> 1) ^ ((crc & 1) ? (CRCXX_POLY) : 0);                \
  35.             for (i = 0; i < 0x100; i += 2 * oneBit)                            \
  36.                 (crcXXTable)[i + oneBit] = (crcXXTable)[i] ^ crc;            \
  37.         }                                                                    \
  38.     }
  39.  
  40. #define DEFINE_CALC_CRC_FUNC(CalculateCRCXX, wordXX, crcXXTable)            \
  41.     wordXX CalculateCRCXX(wordXX crc, byte const *buffer, size_t length)    \
  42.     {                                                                        \
  43.         while (length--)                                                    \
  44.             crc = (crc >> 8) ^ (crcXXTable)[(crc & 0xFF) ^ (*buffer++)];    \
  45.         return crc;                                                            \
  46.     }
  47.  
  48. #define DEFINE_REV_CRC_FUNC(ReverseCRCXX, CRCXX_POLY, wordXX, highBitXX)    \
  49.     wordXX ReverseCRCXX(wordXX crc, byte b)                                    \
  50.     {                                                                        \
  51.         int i;                                                                \
  52.                                                                             \
  53.         for (i = 0; i < 8; i++) {                                            \
  54.             if (crc & (highBitXX))        /* highBitXX is 2^XX - 1 */            \
  55.                 crc = ((crc ^ CRCXX_POLY) << 1) ^ 1;                        \
  56.             else                                                            \
  57.                 crc <<= 1;                                                    \
  58.         }                                                                    \
  59.         return crc ^ b;                                                        \
  60.     }
  61.  
  62. DECLARE_CRC_TABLE(word32, crc32Table)
  63. DEFINE_INIT_CRC_TABLE_FUNC(InitCRC32Table, CRC32_POLY, word32, crc32Table)
  64. DEFINE_CALC_CRC_FUNC(CalculateCRC32, word32, crc32Table)
  65. DEFINE_REV_CRC_FUNC(ReverseCRC32, CRC32_POLY, word32, 0x80000000)
  66.  
  67. DECLARE_CRC_TABLE(word16, crc16Table)
  68. DEFINE_INIT_CRC_TABLE_FUNC(InitCRC16Table, CRC16_POLY, word16, crc16Table)
  69. DEFINE_CALC_CRC_FUNC(CalculateCRC16, word16, crc16Table)
  70. DEFINE_REV_CRC_FUNC(ReverseCRC16, CRC16_POLY, word16, 0x8000)
  71.  
  72. void InitCRC(void)
  73. {
  74.     InitCRC16Table();
  75.     InitCRC32Table();
  76. }
  77.  
  78. /*
  79.  * Local Variables:
  80.  * tab-width: 4
  81.  * End:
  82.  * vi: ts=4 sw=4
  83.  * vim: si
  84.  */
  85.